(function () {
	angular.module('app.mainPage1', ['mgcrea.ngStrap'])
	.controller('mainPageCtrl1',function($scope,$rootScope,$tab,$http){
		$scope.dwLibrary=dwLibrary.elementData;
		$scope.pageButtons=pageButtons;
		$scope.dwpage=dwpage;
		$scope.htmlSource=htmlSource;
		$scope.dataParam=dataParam;
	    
	    $scope.load = function () {
	    	$http({
	    		 method: 'POST',
	    		 url: '/smart-dms/datawindowlist.json',
	    		 headers: {
	    		  // 'Content-Type': undefined
	    		 },
	    		 params: { pageSize: $scope.pageSize,page:$scope.currentPage,pagePath:dwpage.pagePath,dataParam:dataParam },
	    		}).success(function(response){
	    			$scope.assemble(response);
	    		}).error(function(response){
	    			console.log(response);
	    		});
	    };
	    //$scope.load();
	    $scope.next = function () {
	        if ($scope.currentPage < $scope.totalPage) {
	            $scope.currentPage++;
	            $scope.load();
	        }
	    };
	 
	    $scope.prev = function () {
	        if ($scope.currentPage > 1) {
	            $scope.currentPage--;
	            $scope.load();
	        }
	    };
	 
	    $scope.loadPage = function (page) {
	        $scope.currentPage = page;
	        $scope.load();
	    };
	    
	    //组织页面数据
	    $scope.assemble=function(response){
	    	console.log(response);
			 $scope.items = response.data.rows;
            //获取总页数
			$scope.total=response.data.total 
			$scope.pageSize = response.data.pageSize;
            $scope.totalPage = Math.ceil(response.data.total / $scope.pageSize);
            $scope.endPage = $scope.totalPage;
            $scope.currentPage=response.data.page;
            //生成数字链接
            if ($scope.currentPage > 1 && $scope.currentPage < $scope.totalPage) {
                $scope.pages = [
                    $scope.currentPage - 1,
                    $scope.currentPage,
                    $scope.currentPage + 1
                ];
            } else if ($scope.currentPage == 1 && $scope.totalPage > 1) {
                $scope.pages = [
                    $scope.currentPage,
                    $scope.currentPage + 1
                ];
            } else if ($scope.currentPage == $scope.totalPage && $scope.totalPage > 1) {
                $scope.pages = [
                    $scope.currentPage - 1,
                    $scope.currentPage
                ];
            }
	    }
	    $scope.assemble(dataList);
	    
	 function doAdd(){
		 alert($scope.currentPage);
	 }
	    
	    $scope.doEvent =function(event){
	    	doAdd();
	    }
	}).directive('tableRows', ['$http', '$templateCache', '$compile', function($http, $templateCache, $compile) {
		 return {
             restrict: 'A',
             link: function (scope, element, attrs) {
            	 var tpl = $templateCache.get('colTpl');
            	 var pScope = scope.$new();
            	 pScope.cols=scope.items[attrs.rowIndex];
            	var comEle = $compile(tpl)(pScope);
            	element.html(comEle);
             }
         }
	}]);
})();